package com.ruoyi.question.service.impl;

import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.question.domain.EQuestion;
import com.ruoyi.question.mapper.EQuestionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.question.mapper.EQuestionCategorizationMapper;
import com.ruoyi.question.domain.EQuestionCategorization;
import com.ruoyi.question.service.IEQuestionCategorizationService;

/**
 * 问题分类Service业务层处理
 *
 * @author ruoyi
 * @date 2024-11-26
 */
@Service
public class EQuestionCategorizationServiceImpl extends ServiceImpl<EQuestionCategorizationMapper,EQuestionCategorization> implements IEQuestionCategorizationService
{
    @Autowired
    private EQuestionMapper eQuestionMapper;

    @Autowired
    private EQuestionCategorizationMapper eQuestionCategorizationMapper;

    /**
     * 查询问题分类
     *
     * @param id 问题分类主键
     * @return 问题分类
     */
    @Override
    public EQuestionCategorization selectEQuestionCategorizationById(Long id)
    {
        return eQuestionCategorizationMapper.selectEQuestionCategorizationById(id);
    }

    /**
     * 查询问题分类列表
     *
     * @param eQuestionCategorization 问题分类
     * @return 问题分类
     */
    @Override
    public List<EQuestionCategorization> selectEQuestionCategorizationList(EQuestionCategorization eQuestionCategorization)
    {
        return eQuestionCategorizationMapper.selectEQuestionCategorizationList(eQuestionCategorization);
    }

    /**
     * 新增问题分类
     *
     * @param eQuestionCategorization 问题分类
     * @return 结果
     */
    @Override
    public int insertEQuestionCategorization(EQuestionCategorization eQuestionCategorization)
    {
        eQuestionCategorization.setReferencedStatus(Long.valueOf(0));
        eQuestionCategorization.setCreateTime(DateUtils.getNowDate());
        eQuestionCategorization.setQuestionCategorizationStatus(Long.valueOf(0));
        return eQuestionCategorizationMapper.insertEQuestionCategorization(eQuestionCategorization);
    }

    /**
     * 修改问题分类
     *
     * @param eQuestionCategorization 问题分类
     * @return 结果
     */
    @Override
    public int updateEQuestionCategorization(EQuestionCategorization eQuestionCategorization)
    {
        eQuestionCategorization.setLastModifiedTime(new Date());
        return eQuestionCategorizationMapper.updateEQuestionCategorization(eQuestionCategorization);
    }

    /**
     * 批量删除问题分类
     *
     * @param ids 需要删除的问题分类主键
     * @return 结果
     */
    @Override
    public int deleteEQuestionCategorizationByIds(Long[] ids)
    {
        return eQuestionCategorizationMapper.deleteEQuestionCategorizationByIds(ids);
    }

    /**
     * 删除问题分类信息
     *
     * @param id 问题分类主键
     * @return 结果
     */
    @Override
    public int deleteEQuestionCategorizationById(Long id)
    {
        return eQuestionCategorizationMapper.deleteEQuestionCategorizationById(id);
    }

    /*
    * 查询问题分类中文名称列表
    * */
    @Override
    public List<EQuestionCategorization> selectChineseNomenclatureList() {
        return eQuestionCategorizationMapper.selectChineseNomenclatureList();
    }

    /*
    * 修改问题分类的状态
    * */
    @Override
    public int updateEQuestionCategorizationStatus(EQuestionCategorization eQuestionCategorization) {
        List<EQuestion> questionList=eQuestionMapper.selectList(new LambdaQueryWrapper<EQuestion>().eq(EQuestion::getCid,eQuestionCategorization.getId()));
        if(eQuestionCategorization.getQuestionCategorizationStatus()==0){
            eQuestionCategorization.setQuestionCategorizationStatus(Long.valueOf(1));
            for(EQuestion eQuestion:questionList){
                eQuestion.setQuestionStatus(Long.valueOf(1));
                eQuestionMapper.updateEQuestion(eQuestion);
            }
        }else{
            eQuestionCategorization.setQuestionCategorizationStatus(Long.valueOf(0));
            for(EQuestion eQuestion:questionList){
                eQuestion.setQuestionStatus(Long.valueOf(0));
                eQuestionMapper.updateEQuestion(eQuestion);
            }
        }
        return eQuestionCategorizationMapper.updateEQuestionCategorization(eQuestionCategorization);
    }

    /*
    * 修改问题分类的引用状态
    * */
    @Override
    public int updateCategorizationReferenceStatus(Long id) {
        EQuestion eQuestion=eQuestionMapper.selectEQuestionById(id);
        EQuestionCategorization eQuestionCategorization=eQuestionCategorizationMapper.selectEQuestionCategorizationById(eQuestion.getCid());
        Integer count=eQuestionMapper.selectEQuestionCategorizationByCid(eQuestion.getCid());
        if(count==0||count==1){
            eQuestionCategorization.setReferencedStatus(Long.valueOf(0));
        }
        eQuestionCategorization.setLastModifiedTime(new Date());
        return eQuestionCategorizationMapper.updateEQuestionCategorization(eQuestionCategorization);
    }
}
